<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Dbstl examples</title>
    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
    <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" />
    <link rel="up" href="stl.html" title="Chapter 8. Standard Template Library API" />
    <link rel="prev" href="stl_usecase.html" title="Dbstl typical use cases" />
    <link rel="next" href="stl_db_usage.html" title="Berkeley DB configuration" />
  </head>
  <body>
    <div xmlns="" class="navheader">
      <div class="libver">
        <p>Library Version 12.1.6.2</p>
      </div>
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">Dbstl examples</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="stl_usecase.html">Prev</a> </td>
          <th width="60%" align="center">Chapter 8. Standard Template Library API</th>
          <td width="20%" align="right"> <a accesskey="n" href="stl_db_usage.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="sect1" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both"><a id="stl_examples"></a>Dbstl examples</h2>
          </div>
        </div>
      </div>
      <p> 
        Because dbstl is so much like C++ STL, its usage exactly
        mirrors that of C++ STL, with the exception of a few optional
        Berkeley DB specific configurations. In fact, the only
        difference between a program using dbstl and one using C++ STL
        is the class names. That is, <code class="classname">vector</code>
        becomes <code class="classname">db_vector</code>, and
        <code class="classname">map</code> becomes
        <code class="classname">db_map</code>.
    </p>
      <p> 
        The typical procedure for using dbstl is:
    </p>
      <div class="orderedlist">
        <ol type="1">
          <li>
            <p>
                Optionally create and open your own Berkeley DB
                environment and database handles using the DB C++ API.
                If you perform these opens using the C++ API, make
                sure to perform necessary environment and database
                configurations at that time. 
            </p>
          </li>
          <li>
            <p> 
                Optionally pass environment and database handles to
                dbstl container constructors when you create dbstl
                container objects. Note that you can create a dbstl
                container without passing it an environment and
                database object. When you do this, an internal
                anonymous database is created for you. In this
                situation, dbstl provides no data persistence
                guarantees.
            </p>
          </li>
          <li>
            <p> 
                Perform dbstl-specific configurations. For example,
                you can configure cursor open flags, as well as
                database access for autocommit. You can also configure
                callback functions. 
            </p>
          </li>
          <li>
            <p> 
                Interact with the data contained in your Berkeley
                DB databases using dbstl containers and iterators.
                This usage of dbstl is identical to C++ STL container
                and iterator usage.
            </p>
          </li>
          <li>
            <p>
                At this time, you can also use dbstl calls that are
                specific to Berkeley DB. For example, you can use
                Berkeley DB specific calls that manage transaction
                begin/commit/abort, handle registration, and so forth.
                While these calls are part of dbstl, they have no
                equivalence in the C++ STL APIs.
            </p>
          </li>
          <li>
            <p> When your application is done using Berkeley DB,
                you do not need to explicitly close any Berkeley DB
                handles (environments, database, cursors, and so
                forth). Dbstl automatically closes all such handles
                for you. </p>
          </li>
        </ol>
      </div>
      <p>
        For examples of dbstl usage, see the example programs in
        the <code class="filename">$db/examples/stl</code> directory. 
    </p>
      <p> 
        The following program listing provides two code fragments.
        You can find more example code in the
        <code class="filename">dbstl/examples/</code> and
        <code class="filename">dbstl/test</code> directories.
    </p>
      <pre class="programlisting">//////////////// Code Snippet 1 ////////////////
db_vector&lt;int, ElementHolder&lt;int&gt; &gt; vctr(100);
for (int i = 0; i &lt; 100; i++)
    vctr[i] = i;

for (int i = 0; i &lt; 100; i++) {
	cout&lt;&lt;"\nvctr["&lt;&lt;i&lt;&lt;"] : "&lt;&lt;vctr[i];
	vctr[i] = vctr[i] * vctr[i];
	cout&lt;&lt;"\nvctr["&lt;&lt;i&lt;&lt;"] squre : "&lt;&lt;vctr[i];
}

//////////////// Code Snippet 2 ////////////////
typedef db_map&lt;char *, const char *, ElementHolder&lt;const char *&gt; &gt; 
        strmap_t2;
strmap_t2 strmap;
char str[2], str2[2];
str[1] = str2[1] = '\0';
for (char c = 0; c &lt; 26; c++) {
    str[0] = c + 'a';
    str2[0] = 'z' - c;
    strmap[str] = str2;
}
for (strmap_t2::iterator itr = strmap.begin(); itr != strmap.end(); ++itr)
    cout&lt;&lt;endl&lt;&lt;itr-&gt;first&lt;&lt;" : "&lt;&lt;itr-&gt;second;

using namespace dbstl;
dbstl::db_map&lt;char, int&gt; v;
v['i'] = 1;
cout&lt;&lt;v['i'];


dbstl::db_map&lt;char *, char *&gt; name_addr_map;
// The strings rather than the memory pointers are stored into DB.
name_addr_map["Alex"] = "Sydney Australia";
name_addr_map["David"] = "Shenzhen China";
cout&lt;&lt;"Alex's address:"&lt;&lt;name_addr_map["Alex"];

dbstl::db_vector&lt;Person&gt; vi; 
// Some callback configurations follow here.

// The strings and objects rather than pointers are stored into DB.

Person obj("David Zhao", "Oracle", new Office("Boston", "USA"));
vi.push_back(obj); // More person storage.
for (int I = 0; I &lt; vi.size(); I++)
	cout&lt;&lt;vi[I];  </pre>
      <p>
        The first snippet initializes a db_vector container of 100
        elements, with an in-memory anonymous database internally
        created by dbstl. The only difference between this and C++ STL
        is dbstl requires one more <code class="literal">type</code> parameter:
        <em class="parameter"><code>ElementHolder&lt;int&gt;</code></em>. The
        <code class="classname">ElementHolder</code> class template should
        be used for every type of dbstl container that will store C++
        primitive data types, such as <code class="literal">int</code>,
       <code class="literal">float</code>, <code class="literal">char *</code>,
       <code class="literal">wchar_t *</code>, and so forth. But these
        class templates should not be used for class types for reasons
        that we explain in the following chapters.
    </p>
      <p> 
        In the second code snippet, the assignment: 
    </p>
      <pre class="programlisting">strmap[str] = str2;</pre>
      <p>
        is used to store a string pair (<code class="literal">(str,
        str2)</code>) instead of pointers to the underlying
        database. 
    </p>
      <p>
        The rest of the code used in these snippets is identical to
        the code you would use for C++ STL containers. However, by
        using dbstl, you are storing data into a Berkeley DB database.
        If you create your own database with backing files on disk,
        your data or objects can persist and be restored when the
        program runs again.
    </p>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="stl_usecase.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="stl.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="stl_db_usage.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">Dbstl typical use cases </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> Berkeley DB configuration</td>
        </tr>
      </table>
    </div>
  </body>
</html>
